/**
 * Copyright 2010 Butin Kirill <kiryaka@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.flexupload.remote
{
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	
	import org.flexupload.strategy.DevStrategy;

	public dynamic class FlexuploadService extends RemoteObject
	{
		protected var strategy:DevStrategy;
		private var listeners:Array = new Array();
		public function FlexuploadService(destination:String = null)
		{
			super(destination);
//			this.addEventListener(FaultEvent.FAULT, faultErrorHandler);
//			this.addEventListener(ResultEvent.RESULT, resultErrorHandler);
		}
//		private function faultErrorHandler(event:FaultEvent):void{
//			Alert.show(event.toString());
//		}
//
//		private function resultErrorHandler(event:ResultEvent):void{ 
//			Alert.show("sadfasd");
//		}		
		
		public function setStrategy(strategy:DevStrategy):void{
			this.strategy = strategy;
		}
		override public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false) : void{
			listeners.push(type);
			super.addEventListener(type, listener, useCapture, priority, useWeakReference);
		}
		
		override public function removeEventListener(type:String, listener:Function, useCapture:Boolean=false) : void{
			listeners.pop();
			super.removeEventListener(type, listener, useCapture);
		}
		public function markServiceAsFree():void{
			if (listeners.length > 0) throw new Error("We want free remote service but have listeners here..." + listeners.length);
			strategy.markServiceAsFree(this);
		}
	}
}